﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Globalization;

namespace MC34063_Universal_Calculator
{
    [ValueConversion(typeof(double), typeof(double))]
    public class MultipleValueConverter :IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double multiplyer = 0;
            double.TryParse(parameter.ToString(), out multiplyer);
            return System.Convert.ToDouble(value) * multiplyer;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double multiplyer = 0;
            double.TryParse(parameter.ToString(), out multiplyer);
            return System.Convert.ToDouble(value) / multiplyer;
        }
    }

    [ValueConversion(typeof(double), typeof(string))]
    public class DoubleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int decimation = 0;
            int.TryParse(parameter.ToString(), out decimation);
            double dValue = System.Convert.ToDouble(value);
            int decimals = (int)((dValue - Math.Floor(dValue)) * 1000);
            int digit3, digit2, digit1;

            digit3 = decimals / 100;
            digit2 = (decimals % 100) / 10;
            digit1 = (decimals % 10);

            decimation = ((digit1 == 0) && (decimation == 3)) ? 2 : decimation;
            decimation = ((digit2 == 0) && (decimation == 2)) ? 1 : decimation;
            decimation = ((digit3 == 0) && (decimation == 1)) ? 0 : decimation;

            return dValue.ToString("F" + decimation.ToString(), culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return System.Convert.ToDouble(value);
        }
    }

    [ValueConversion(typeof(double), typeof(string))]
    public class StringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string[] parameters = parameter.ToString().Split(';');

            return ((double)value).ToString("N" + parameters[0], culture) + ((parameters.Length > 1) ? parameters[1] : "");
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

//(c) 2011 - Tobias Eiseler

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>